 /*      SClassFile.cpp
 *
 *      Copyright 2009-2010 virpool <virpool@ukrpost.ua>
 *
 *      This program is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation; either version 2 of the License, or
 *      (at your option) any later version.
 *
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *
 *      You should have received a copy of the GNU General Public License
 *      along with this program; if not, write to the Free Software
 *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 *      MA 02110-1301, USA.
 */

#include "SClassFile.h"
#include "SClassString.h"
#include <cstring>

SClassFile::SClassFile(string name): SObject(name, "File")
{
	// TODO Auto-generated constructor stub
	//GetMethods()->insert(pair<string, s_func>("ToString", &SClassInteger::ToString));
	GetMethods()->insert(pair<string, s_func>("OpenExists", &SClassFile::OpenExists));
	GetMethods()->insert(pair<string, s_func>("CreateOpen", &SClassFile::CreateOpen));
	GetMethods()->insert(pair<string, s_func>("WriteLine", &SClassFile::WriteLine));
	GetMethods()->insert(pair<string, s_func>("ReadLine", &SClassFile::ReadLine));
	GetMethods()->insert(pair<string, s_func>("Eof", &SClassFile::Eof));
	GetMethods()->insert(pair<string, s_func>("Close", &SClassFile::Close));
	GetMethods()->insert(pair<string, s_func>("Good", &SClassFile::Good));

}

SClassFile::~SClassFile()
{
	// TODO Auto-generated destructor stub
}

int SClassFile::ToString(SObject* obj, s_param* res, param_list* args)
{

}

int SClassFile::OpenExists(SObject* obj, s_param* res, param_list* args)
{
	static string r = "openexists ok\n";
	if (obj == NULL || args == NULL || args->size() == 0)
		return S_INVOKE_INCORRECT;
	param_list::iterator p = args->begin();
	if (p->type != S_STRING)
		return S_INVOKE_INCORRECT;
	((SClassFile*)obj)->f.open((*((string*)p->val)).c_str(), ios::app | ios::in | ios::out);
	((SClassFile*)obj)->f.seekp(ios::beg);
	if (res) {
		res->type = S_STRING;
		res->val = &r;
	}
	return S_INVOKE_OK;

}

int SClassFile::CreateOpen(SObject* obj, s_param* res, param_list* args)
{
	if (obj == NULL || args == NULL || args->size() == 0)
		return S_INVOKE_INCORRECT;
	param_list::iterator p = args->begin();
	if (p->type != S_STRING)
		return S_INVOKE_INCORRECT;
	((SClassFile*)obj)->f.open((*((string*)p->val)).c_str(), ios::trunc | ios::in | ios::out);
	((SClassFile*)obj)->f.seekp(ios::beg);
	return S_INVOKE_OK;
}

int SClassFile::WriteLine(SObject* obj, s_param* res, param_list* args)
{
	if (obj == NULL || args == NULL || args->size() == 0)
		return S_INVOKE_INCORRECT;
	param_list::iterator p = args->begin();
	if (p->type != S_STRING)
		return S_INVOKE_INCORRECT;
	((SClassFile*)obj)->f.write((*((string*)p->val)).c_str(), (*((string*)p->val)).size() );
	return S_INVOKE_OK;
}

int SClassFile::ReadLine(SObject* obj, s_param* res, param_list* args)
{
	if (obj == NULL || args == NULL || !args->size())
		return S_INVOKE_INCORRECT;
	param_list::iterator p = args->begin();
	if (p->type != S_OBJECT)
		return S_INVOKE_INCORRECT;
	SObject *ob = (SObject*) p->val;
	
	if (ob->GetClassName() != "String")
		return S_INVOKE_INCORRECT;
	
	((SClassFile*)obj)->f.getline(&(((SClassFile*)obj)->buf[0]), 4095);
	
	if (strlen(&(((SClassFile*)obj)->buf[0])))
		((SClassString*)ob)->Set(((SClassFile*)obj)->buf);
	else
		((SClassString*)ob)->Set("");
	
	return S_INVOKE_OK;
}

int SClassFile::Eof(SObject* obj, s_param* res, param_list* args)
{
	static bool teof = true, feof = false;
	if (obj == NULL || res == NULL)
		return S_INVOKE_INCORRECT;
	if (((SClassFile*)obj)->f.eof())
		res->val = &teof;
	else
		res->val = &feof;
	res->type = S_BOOL;
	return S_INVOKE_OK;
}

int SClassFile::Good(SObject* obj, s_param* res, param_list* args)
{
	static bool teof = true, feof = false;
	if (obj == NULL || res == NULL)
		return S_INVOKE_INCORRECT;
	if (((SClassFile*)obj)->f.fail())
		res->val = &feof;
	else
		res->val = &teof;
	res->type = S_BOOL;
	return S_INVOKE_OK;
}

int SClassFile::Close(SObject* obj, s_param* res, param_list* args)
{
	if (obj == NULL || res == NULL)
		return S_INVOKE_INCORRECT;
	((SClassFile*)obj)->f.close();
	return S_INVOKE_OK;
}
